package com.example.tests;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.example.utils.SortedListOf;

import static com.example.fw.ContactHelper.CREATION;
import static com.example.tests.ContactDataGenerator.loadContactsFromCsvFile;
import static com.example.tests.ContactDataGenerator.loadGroupsFromXmlFile;

public class ContactCreationTests extends TestBase {

	@DataProvider
	public Iterator<Object[]> contactsFromFile() throws IOException{
		return wrapContactsForDataProvider(loadContactsFromCsvFile(new File("contacts.txt"))).iterator();
	}
	
	@Test(dataProvider = "contactsFromFile")
	public void createNoEmptyContact( ContactData contact ) throws Exception {
		
		// save old state
		SortedListOf<ContactData> oldList = app.getModel().getContact();

		// do some action
		app.getContactHelper().createContact(contact, CREATION);
		
		// save a new state
		SortedListOf<ContactData> newList = app.getModel().getContact();
		
		// compare old and new state
		assertThat(newList, equalTo(oldList.withAdded(contact)));
		
		if ( wantToCheck() ){
			if ("yes".equals(app.getProperty("check.db")))
				assertThat(app.getModel().getContact(), equalTo(app.getHibernateHelper().listContact()));
			
			if ("yes".equals(app.getProperty("check.gui")))
				assertThat(app.getModel().getContact(), equalTo(app.getContactHelper().getUiContacts()));
		}
	}
}
